<?php

namespace app\model;

use think\Db;

class UserInfo extends LoginCommon
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'user_info';
    }

    /**
     * 类入口
     * @return UserInfo
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 快速注册
     */
    public function fastRegister($str_mobile)
    {
        $arr_data = [
            'mobile' => $str_mobile,
            'gmt_create' => $this->_now,
        ];
        return $this->toAdd($arr_data);
    }


    /**
     * 获取会员信息
     * $arr_where, $str_field, $int_first_row, $int_list_rows, $str_order
     */
    public function getUserList($int_first_row,$int_list_rows,$arr_where,$sort){

       $data =  Db::name($this->db_name)
                ->where($arr_where)
                ->field('user_info.user_id as userId,user_info.nickname as nickName,user_info.mobile,count(user_order.user_id) as cnt,ifnull(sum(user_order.order_amount),0) as amount,user_info.gmt_create as gmtCreate,user_info.is_enable as isEnable,user_info.real_name as realName')
                ->join('user_order','user_order.user_id = user_info.user_id','left')
                ->limit($int_first_row,$int_list_rows)
                ->group('user_info.user_id')
                ->order("user_info.gmt_create $sort")
                ->select();
        return $data;
    }



    /**
     * 获取会员详情信息
     */
    public function getUserDetailList($arr_where){
        $detailData = Db::name($this->db_name)
                ->where($arr_where)
                ->field('user_id as userId,nickname,gmt_create as gmtCreate,real_name as realName,sex,birthday,mobile,head_portrait as headPortrait')
                ->select();
        return $detailData;
    }

    /**
     * 获取统计信息
     */
    public function getStatistical($arr_where){
        $data = Db::name($this->db_name)
            ->where($arr_where)
            ->field('ifnull(sum(user_order.order_amount),0) as orderAmount,sum(user_order.user_id) as cnt,ifnull(sum(case when user_order.order_state =0 then 1 else 0 end),0) as cancel')
            ->join('user_order','user_order.user_id=user_info.user_id','left')
            ->group('user_info.user_id')
            ->select();
        $loginNumber = Db::name($this->db_name)
            ->where($arr_where)
            ->field('count(user_passport.user_id) as loginNumber')
            ->join('user_passport','user_passport.user_id=user_info.user_id')
            ->group('user_info.user_id')
            ->select();
        $data[0]['loginNumber']=$loginNumber[0]['loginNumber'];
        return $data;
    }

    /**
     *  获取实名认证材料信息
     */
    public function getCertification($arr_where){
        $data = Db::name($this->db_name)
            ->where($arr_where)
            ->field('real_name as realName,id_number as idNumber,start_term as startTerm,end_term as endTerm,idcard_front as idcardFront,idcard_back as idcardBack,head_portrait as headPortrait')
            ->select();
        return $data;
    }

    /**
     * 获取寄件人地址簿
     */
    public function getSendAddress($arr_where){
        $sendData = Db::name($this->db_name)
            ->where($arr_where)
            ->field('user_address.id,user_address.name,user_address.phone,user_address.prov_name as provName,user_address.city_name as cityName,user_address.county_name as countyName,user_address.street_name as streetName,user_address.detail_address as detailAddress')
            ->join('user_address','user_address.user_id=user_info.user_id')
            ->select();
        return $sendData;
    }
    /**
     * 获取收件人地址簿
     */
    public function getReceiveAddress($arr_where){
        $sendData = Db::name($this->db_name)
            ->where($arr_where)
            ->field('user_address.id,user_address.name,user_address.phone,user_address.prov_name as provName,user_address.city_name as cityName,user_address.county_name as countyName,user_address.street_name as streetName,user_address.detail_address as detailAddress')
            ->join('user_address','user_address.user_id=user_info.user_id')
            ->select();
        return $sendData;
    }

    /**
     * 会员资料编辑
     */
    public function getEditData($arr_where){
        $data = Db::name($this->db_name)
            ->where($arr_where)
            ->field('user_id,mobile,real_name as realName,id_number as idNumber,start_term as startTerm,end_term as endTerm,idcard_front as idcardFront,idcard_back as idcardBack,head_portrait as headPortrait,is_enable as isEnable')
            ->select();
        return $data;
    }

    /**
     * @param $arr_user_ids
     * @return array
     */
    public function noPaymentTimeUser($arr_user_ids){
        return  Db::name($this->db_name)->where(['user_id' => ['NOT IN', $arr_user_ids]])->column('user_id');
    }
}
